<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/style.css" rel="stylesheet" />
		<style>
			.mui-bar {
				height: 3em;
				font-weight: bolder;
				background-color: #efeff4;
				font-family: "黑体";
			}
			
			.sub-tittle {
				line-height: 4em;
				top: 1.2em;
				font-size: .7em;
				font-family: elephant;
			}
			
			.img-signin img {
				width: 30%;				
				padding: 10% 0;
			}

			.img-signin {
				padding: 20% 0;
				height: 40%;
				text-align: center;
			}
			
			.area {
				margin: 20px auto 0px auto;
			}
			
			#forgetPassword {
				float: right;
				margin-right: .5em;
				font-size: .7em;
			}
			
			#savePassword {
				height: 21px;
				line-height: 21px;
				font-size: .7em;
				color: #007aff;
				float: left;
			}
			#savePassword input{
				vertical-align: middle;
			}
			
			.mui-input-group {
				margin: 10px 15px;
				border-radius: 5px;
				padding-left: 2em;
			}
			
			.mui-input-group span {
				position: absolute;
				left: 1px;
				top: -4px;
			}
			
			.mui-input-group:first-child {
				margin-top: 20px;
			}
			
			.mui-input-group label {
				width: 22%;
			}
			
			.mui-input-row label~input,
			.mui-input-row label~select,
			.mui-input-row label~textarea {
				width: 78%;
			}
			
			.mui-input-row span {
				line-height: 1.1;
				float: left;
				width: 35%;
				padding: 11px 15px;
			}
			
			.mui-input {
				padding-top: 1em;
			}
			
			.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}
			
			.mui-content-padded {
				margin-top: 25px;
			}
			
			.mui-btn {
				padding: 10px;
				background: #378DE4;
				border: 0;
				letter-spacing:.2em;
			}
			
			.link-area {
				display: block;
				height: 3em;
			}
			
			.link-area input {
				width: 1em;
				background-color: #fff;
			}
			
			.spliter {
				color: #bbb;
				padding: 0px 8px;
			}
			
			.oauth-area {
				position: absolute;
				bottom: 20px;
				left: 0px;
				text-align: center;
				width: 100%;
				padding: 0px;
				margin: 0px;
			}
			
			.oauth-area .oauth-btn {
				display: inline-block;
				width: 50px;
				height: 50px;
				background-size: 30px 30px;
				background-position: center center;
				background-repeat: no-repeat;
				margin: 0px 20px;
				/*-webkit-filter: grayscale(100%); */
				border: solid 1px #ddd;
				border-radius: 25px;
			}
			
			.oauth-area .oauth-btn:active {
				border: solid 1px #aaa;
			}
			
			.oauth-area .oauth-btn.disabled {
				background-color: #ddd;
			}
		</style>

	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<h1 class="mui-title">空压机</h1>
			<h5 class="mui-title sub-tittle">Air Compressor</h5>
		</header>
		
		<div class="mui-content">
			<div class="img-signin">
				<img src="images/signin.png" />
			</div>
			<div class="mui-input-row mui-input-group">
				<span class="mui-icon mui-icon-contact"></span>
				<input id='username' type="text" class="mui-input mui-input" placeholder="Username">
			</div>
			<div class="mui-input-row mui-input-group">
				<span class="mui-icon mui-icon-locked"></span>
				<input id='password' type="password" class="mui-input mui-input" placeholder="Password">
			</div>
			<div class="mui-content-padded">
				<div class="link-area">
					<div id="savePassword" class=" mui-left">
						<input type="checkbox" name="autoLogin" id="autoLogin" value="false" />
						<label>记住密码<label>
					</div>			
					<a id='forgetPassword'>忘记密码?</a>
				</div>
				<button id='login' class="mui-btn mui-btn-block mui-btn-primary">登录</button>
			</div>
			<div class="mui-content-padded oauth-area">

			</div>
		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/mui.enterfocus.js"></script>
		<script src="js/app.js"></script>
		<script>
			(function($, doc) {
				$.init({
					statusBarBackground: '#f7f7f7'
				});
				
				$.plusReady(function() {
					plus.screen.lockOrientation("portrait-primary");
					var settings = app.getSettings();
					var state = app.getState();
					
//					var mainPage = $.preload({
//						"id": 'main1',
//						"url": 'main.html'
//					});
//					
//					var main_loaded_flag = false;
//					
//					mainPage.addEventListener("loaded",function () {
//						main_loaded_flag = true;
//						
//					});
					var mainPage = $.preload({
						"id": 'main1',
						"url": 'main.html'
					});
					var toMain = function() {
						$.fire(mainPage, 'show', null);
						setTimeout(function() {
							$.openWindow({
								id: 'main1',
								show: {
									aniShow: 'pop-in'
								},
								waiting: {
									autoShow: false
								}
							});
						}, 0);
						plus.webview.close('login');
					}

//					var toMain = function() {
//						//使用定时器的原因：
//						//可能执行太快，main页面loaded事件尚未触发就执行自定义事件，此时必然会失败
//						var id = setInterval(function () {
//							if(main_loaded_flag){
//								clearInterval(id);
//								$.fire(mainPage, 'show', null);
//								mainPage.show("pop-in");
//								plus.webview.close("login");
//							}
//						},20);
//
//					};
					//检查 "登录状态/锁屏状态" 开始
					if (settings.autoLogin && state.token && settings.gestures) {
						$.openWindow({
							url: 'unlock.html',
							id: 'unlock',
							show: {
								aniShow: 'pop-in'
							},
							waiting: {
								autoShow: false
							}
						});
					} else if (settings.autoLogin && state.token) {
						toMain();
					}
					
					//检查 "登录状态/锁屏状态" 结束
					var loginButton = doc.getElementById('login');
					var usernameBox = doc.getElementById('username');
					var passwordBox = doc.getElementById('password');
					var autoLoginButton = doc.getElementById("autoLogin");

					loginButton.addEventListener('tap', function(event) {
						var loginInfo = {
							username: usernameBox.value,
							password: passwordBox.value
						};
						app.login(loginInfo, function(err) {
							if (err) {
								plus.nativeUI.toast(err);
								return;
							}
							toMain();
						});
					});
					
					$.enterfocus('#login-form input', function() {
						$.trigger(loginButton, 'tap');
					});
					
					autoLoginButton.classList[settings.autoLogin ? 'add' : 'remove']('mui-active')
					
					autoLoginButton.addEventListener('toggle', function(event) {
						setTimeout(function() {
							var isActive = event.detail.isActive;
							settings.autoLogin = isActive;
							app.setSettings(settings);
						}, 50);
					}, false);

					//
					var backButtonPress = 0;
					$.back = function(event) {
						backButtonPress++;
						if (backButtonPress > 1) {
							plus.runtime.quit();
						} else {
							plus.nativeUI.toast('再按一次退出应用');
						}
						setTimeout(function() {
							backButtonPress = 0;
						}, 1000);
						return false;
					};
				});
			}(mui, document));
		</script>
	</body>

</html>